package cn.iocoder.yudao.module.biz.service.trainingRecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.trainingRecord.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.trainingRecord.BizTrainingRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.trainingRecord.BizTrainingRecordConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.trainingRecord.BizTrainingRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 培训记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizTrainingRecordServiceImpl implements BizTrainingRecordService {

    @Resource
    private BizTrainingRecordMapper trainingRecordMapper;

    @Override
    public String createTrainingRecord(BizTrainingRecordCreateReqVO createReqVO) {
        // 插入
        BizTrainingRecordDO trainingRecord = BizTrainingRecordConvert.INSTANCE.convert(createReqVO);
        trainingRecordMapper.insert(trainingRecord);
        // 返回
        return trainingRecord.getId();
    }

    @Override
    public void updateTrainingRecord(BizTrainingRecordUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateTrainingRecordExists(updateReqVO.getId());
        // 更新
        BizTrainingRecordDO updateObj = BizTrainingRecordConvert.INSTANCE.convert(updateReqVO);
        trainingRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteTrainingRecord(String id) {
        // 校验存在
        this.validateTrainingRecordExists(id);
        // 删除
        trainingRecordMapper.deleteById(id);
    }

    private void validateTrainingRecordExists(String id) {
        if (trainingRecordMapper.selectById(id) == null) {
            throw exception(TRAINING_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public BizTrainingRecordDO getTrainingRecord(String id) {
        return trainingRecordMapper.selectById(id);
    }

    @Override
    public List<BizTrainingRecordDO> getTrainingRecordList(Collection<String> ids) {
        return trainingRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizTrainingRecordDO> getTrainingRecordPage(BizTrainingRecordPageReqVO pageReqVO) {
        return trainingRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizTrainingRecordDO> getTrainingRecordList(BizTrainingRecordExportReqVO exportReqVO) {
        return trainingRecordMapper.selectList(exportReqVO);
    }

    @Override
    public List<BizTrainingRecordPageJoinVO> selectTrainingRecord(BizTrainingRecordPageReqVO pageVO){
        return trainingRecordMapper.selectTrainingRecord(pageVO);
    }

    @Override
    public Integer  selectTrainingRecordCount(BizTrainingRecordPageReqVO pageVO){
        return trainingRecordMapper.selectTrainingRecordCount(pageVO);
    }

}
